//
//  DBJPBenefitViewController.m
//  DBJ
//
//  Created by 花化华 on 2016/11/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJPBenefitViewController.h"
#import "DBJNetworkTool.h"
#import "SVProgressHUD.h"

@interface DBJPBenefitViewController ()

//WebView
@property(nonatomic,strong)UIWebView *webView;
//NSURLSession
@property (nonatomic,strong)NSURLSession *session;

@end

@implementation DBJPBenefitViewController

- (void)dbjSetupSubviews{
    [super dbjSetupSubviews];
    
//    [self.view addSubview:self.webView];
    
    //创建webView
    UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.webView setBackgroundColor:[UIColor zbh_colorWithRed:239 green:239 blue:243]];
    self.webView = webView;
    [self.view addSubview:webView];
    
    //加载数据
    [self loadData];
}

#pragma mark - 懒加载
- (NSURLSession *)session{
    if (_session == nil) {
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        //设置配置信息
        config.HTTPAdditionalHeaders = @{
                                         @"User-Agent":@"iPhone"
                                         };
        _session = [NSURLSession sessionWithConfiguration:config];
        
    }
    return _session;
}

#pragma mark - 加载数据
- (void)loadData{
    
    //数据源地址URL
    NSURL *url = [NSURL URLWithString:@"http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm"];
    
    //弹框提示
    [DBJTools dbjShowSVTitle:@"加载中..." andTime:STANTIME];
    
    //用全局session开启任务,添加任务
    NSURLSessionDataTask *dataTask = [self.session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //处理响应
        if (error == nil && data != nil && data.length > 0) {
            //反序列化
            //把data数据转换成OC字符串对象
            NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                //刷新数据
                [self.webView loadHTMLString:html baseURL:url];
            }];
        }else{
            //打印错误信息
            NSLog(@"错误信息==%@",error);
        }
    }];
    
    //启动任务
    [dataTask resume];
    
}

@end
